import { Component, OnInit, OnDestroy } from '@angular/core';

import { UserHistorysProvider } from './user.historys.provider';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { Util } from '../../../util/util';
import { ManhuaDetailProvider } from '../../manhua/detail/manhua.detail.provider';
import { UserManhuaBase } from '../user.manhua.base';

@Component({
    selector: 'user-historys',
    templateUrl: './user.historys.html',
    styleUrls: ['./user.historys.css'],
    providers: [UserHistorysProvider, ManhuaDetailProvider]
})

export class UserHistorys extends UserManhuaBase {

    historys: any = [];
    manhuaEditBool: boolean = false;

    page: any = {
        size: 12,
        curPage: 1,
        pages: 0,
        totalCount: 1
    };
    constructor(private service: UserHistorysProvider, private manhuaProvider: ManhuaDetailProvider, public util: Util, public router: Router) {
        super(util, router);
    }

    ngOnInit() {
        let json = Object.assign(this.param, this.page);

        this.getHttp(json);
    }


    // page nav
    pageChange(cur) {

        this.page.curPage = cur;

        let json = Object.assign(this.param, this.page);

        // console.log('page page');
        // console.log(json);
        this.getHttp(json);

    }

    manhuaEdit() {
        this.manhuaEditBool = !this.manhuaEditBool;
    }
    cancel() {
        for (let i in this.historys) {
            if (this.historys[i]['deleteBool'] === true) {
                this.historys[i]['deleteBool'] = false;
            }
        }

        this.manhuaEditBool = !this.manhuaEditBool;
    }

    comfirm() {
        for (let i in this.historys) {
            let item = this.historys[i];
            if (item['deleteBool'] === true) {
                this.del(item);
            }
            item = null;
        }
    }



    // 删除
    delAjaxSub: Subscription;
    del(history_manhua) {
        let json = {
            "_id": history_manhua['_id']
        };
        this.delAjaxSub = this.service.historyDel(json).subscribe((res: any) => {
            if (res.success) {
                let json = Object.assign(this.param, this.page);
                this.getHttp(json);
            }
        });
    }

    // 时间筛选
    param: any = {
        "date": ''
    };
    dateSearch(day) {
        this.page = {
            size: 12,
            curPage: 1,
            pages: 0,
            totalCount: 1
        };
        this.param = { "date": day };
        let json = Object.assign(this.param, this.page);

        this.getHttp(json);
    }

    pageSet(_page) {
        this.page.pages = _page.pages;
        this.page.curPage = _page.curPage;
        this.page.totalCount = _page.totalCount;
    }

    ajaxSub: Subscription;
    getHttp(param) {
        this.historys.length = 0;
        this.ajaxSub = this.service.dateSearch(param).subscribe((res: any) => {
            if (res.success) {
                // page init
                this.pageSet(res.page);
                this.historys = res.data;

            } else {
                this.historys = [];
            }
        }, (err: any) => {

        });
    }

    ngOnDestroy() {
        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }


    }
}